package com.ouken.activity;
import java.util.ArrayList;

import com.ouken.tool.Device;
import com.ouken.tool.DeviceslistAdapter;

import device_ctr.Device_ctr;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MySettingsOnlineActivity extends Activity {

	private ImageButton back2;
	private ImageButton back1;

	private Context context;
	private ListView available_device_listView;
	
	private ArrayList<Device> OnlineDeviceLists;

	private String[] contents ;
	private String result = ("QZ0112345670,ﾒｫﾄﾇﾌｨｳｵ,1,1,"
            +"QZ0112345671,ｴ邨ﾄﾐ｡ｺﾚ,4,1,"
            +"QZ0212345672,ﾍｬﾌ�ｰｵﾄﾜ㍽�3,1,"
            +"QZ0412345673,ﾀﾏ邪ｵﾄｷﾀｱIﾏｵｽy,2,1,"
            +"QZ0712345674,ﾐ｡ﾁﾖｼﾒｵﾄ絹ﾖv僂,3,1,"
            +"QZ0881345675,餤ｽ�2,1,"
            +"QZ0312345676,ﾄｦﾍﾐﾜ�2,1,"
            +"QZ0512345677,ﾐ｡刔滬,1,1,"
            +"QZ1012345678,ﾟbｿﾘ餤,2,1,"
            +"QZ0612345679,饑鷄,3,1,"
            +"QZ0912345661,閠ｳﾗ,2,1,"
            +"QZ0212345662,ﾒｫﾄﾇﾌｨｳｵ,5,0,"
            +"QZ0312345663,ﾒｫﾄﾇﾌｨｳｵ,5,0");
	private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
         public void run () {
        	 updateDeviceList();
        	 handler.postDelayed(this,2500);
         }
    };
	private void updateDeviceList(){
		int dataLength;
		Device deviceTmp;
		
		Device_ctr kk=new Device_ctr(context);
		result=kk.Refresh();
		
		if(result == null) return;
		Log.e("Duan", "Refesh list");
		OnlineDeviceLists.clear();
		
		contents = result.split(",");
		dataLength = contents.length/5;
		
		for(int i =0; i<dataLength; i++){
		  deviceTmp = new Device(contents[i*5],contents[i*5+1],Integer.parseInt(contents[i*5+2]),contents[i*5+3],contents[i*5+4]);
		  if(deviceTmp.getIsOnline()) OnlineDeviceLists.add(deviceTmp);
		}
		
		DeviceslistAdapter available_adapter=new DeviceslistAdapter(this,OnlineDeviceLists);  
		available_device_listView.setAdapter(available_adapter);
	}
	private void initView(){
		context = this;
		back1 = (ImageButton)findViewById(R.id.mysettings_online_back1); 
		back2 = (ImageButton)findViewById(R.id.mysettings_online_back2); 
		
		available_device_listView = (ListView)findViewById(R.id.edit_available_device_listView);
		OnlineDeviceLists = new ArrayList<Device>();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edite_device_list);
		initView();
		
		updateDeviceList();
		//*******************************************************
		handler.removeCallbacks(runnable);
		handler.postDelayed(runnable,2500);
		//*******************************************************
		available_device_listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(android.widget.AdapterView<?> arg0,
					android.view.View arg1, int arg2, long arg3) {
				Intent intent ;
				if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_01){
					 intent = new Intent(context, EditDevice01Activity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_02){
					 intent = new Intent(context, EditDevice02Activity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_03){
					 intent = new Intent(context, EditDevice03Activity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_04){
					 intent = new Intent(context, EditDevice04Activity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_05){
					 intent = new Intent(context, EditDevice05Activity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_06){
					 intent = new Intent(context, EditDevice06Activity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_07){
					 intent = new Intent(context, EditDevice07Activity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_08){
					 intent = new Intent(context, EditDevice08Activity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_09){
					 intent = new Intent(context, EditDevice09Activity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_10){
					 intent = new Intent(context, EditDevice10Activity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_11){
					 intent = new Intent(context, EditDevice11Activity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_12){
					 intent = new Intent(context, EditDevice12Activity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_13){
					 intent = new Intent(context, EditDevice13Activity.class); 
				}else if(OnlineDeviceLists.get(arg2).getDeviceName() == R.string.device_name_14){
					 intent = new Intent(context, EditDevice14Activity.class); 
				}else return;
				Device_ctr jj = new Device_ctr(context);
				//jj.AddWifiInfo(OnlineDeviceLists.get(arg2).getDeviceSSID().toString(),"12345678",3);
				jj.Connect_wifi(OnlineDeviceLists.get(arg2).getDeviceSSID().toString());
				//TODO:ﾐ靨ｪｴﾓﾊ�ﾝｿ篏｡ﾃﾜﾂ�ｬﾈｻｺﾑﾕ篋ﾜﾂ�ｫｸﾃｻｧｻｹﾊﾇﾔｴｰ�ｬｻｹﾃｻﾓﾐﾈｷｶｨ
				intent.putExtra("deviceSSID",OnlineDeviceLists.get(arg2).getDeviceSSID().toString());
				//intent.putExtra("deviceBSSID",OnlineDeviceLists.get(arg2).getDeviceSSID().toString());
		        intent.putExtra("deviceDescription",OnlineDeviceLists.get(arg2).getDeviceDescription());
		        intent.putExtra("deviceSignal",OnlineDeviceLists.get(arg2).getDeviceSignal());
		        intent.putExtra("devicePWD",OnlineDeviceLists.get(arg2).getDevicePWD().toString());

		        
		        //devicePWD = intent.getStringExtra("devicePWD").toString();
				//deviceSSID = intent.getStringExtra("deviceSSID").toString();
				//deviceBSSID= intent.getStringExtra("deviceBSSID").toString();
		        startActivity(intent);
			}
		});	
		
		
		
		
		back1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				MySettingsOnlineActivity.this.finish();
			}
		});
		
		back2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				MySettingsOnlineActivity.this.finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}

}
